package cn.lyh.edu.controller;



import cn.lyh.annotation.EnableSysLog;
import cn.lyh.base.BaseResult;

import cn.lyh.edu.domain.*;

import cn.lyh.edu.service.*;
import cn.lyh.edu.service.dto.LessonQueryCriteria;
import cn.lyh.utils.HutoolJWTUtil;
import cn.lyh.utils.PageVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("edu")
public class EduIndexController {
private final IEduNewsService eduNewsService;
private final IEduBannerService eduBannerService;
private final IEduTeacherService eduTeacherService;
private final IEduLessonTypeService eduLessonTypeService;
private final IEduLessonService eduLessonService;
private final IEduMemberService eduMemberService;
    public EduIndexController(IEduNewsService eduNewsService, IEduBannerService eduBannerService, IEduTeacherService eduTeacherService, IEduLessonTypeService eduLessonTypeService, IEduLessonService eduLessonService,IEduMemberService eduMemberService) {
        this.eduNewsService = eduNewsService;
        this.eduBannerService = eduBannerService;
        this.eduTeacherService = eduTeacherService;
        this.eduLessonTypeService = eduLessonTypeService;
        this.eduLessonService = eduLessonService;
        this.eduMemberService= eduMemberService;
    }




//    @EnableSysLog("【前台】获取新闻数据")
    @GetMapping("getIndex")
    public BaseResult getIndex() {
        HashMap<String, Object> resultMap = new HashMap<>();
//        获取新闻
        List<EduNews> indexNewsList = eduNewsService.getAll();
        resultMap.put("indexNewsList", indexNewsList);
        return BaseResult.success(resultMap);
    }


//    @EnableSysLog("【前台】获取轮播图数据")
    @GetMapping("getBanner")
    public BaseResult getBanner() {
        HashMap<String, Object> resultMap = new HashMap<>();

        List<EduBanner> indexBannerList = eduBannerService.getAll();
//        key和value
        resultMap.put("indexBannerList", indexBannerList);

        return BaseResult.success(resultMap);
    }




//    @EnableSysLog("【前台】获取课程首页数据")
    @GetMapping("getLesson")
    public BaseResult getLesson() {
        HashMap<String, Object> resultMap = new HashMap<>();

        List<EduLesson> indexLessonList = eduLessonService.getAll();
//        key和value
        resultMap.put("indexLessonList", indexLessonList);

        return BaseResult.success(resultMap);
    }

//    @EnableSysLog("【前台】获取全部教师和课程类型")
    @GetMapping("getAllLessonTypeAndTeacher")
    public BaseResult getAllLessonTypeAndTeacher(){
        Map<String,Object> resultMap = new HashMap<>();
        // 获取课程类型列表
        List<EduLessonType> lessonTypeList = eduLessonTypeService.getAll();
        resultMap.put("lessonTypeList",lessonTypeList);
        // 获取教师列表
        List<EduTeacher> teacherList = eduTeacherService.getAll();
        resultMap.put("teacherList",teacherList);
        return BaseResult.success(resultMap);
    }
    
    
//    @EnableSysLog("【前台】访问课程详情")
    @GetMapping("/detail/{id}")
    public BaseResult detail(@PathVariable Long id){
        // 获取课程信息
        EduLesson dbEduLesson = eduLessonService.getById(id);
        // 根据楼层ID获取楼层信息
        EduTeacher dbEduTeacher = eduTeacherService.getById(dbEduLesson.getTeacherId());
        dbEduLesson.setEduTeacher(dbEduTeacher);
        return BaseResult.success(dbEduLesson);
    }


//    @EnableSysLog("【前台】获取课程列表")
    @GetMapping("getLessonList")
    public BaseResult getLessonList(LessonQueryCriteria queryCriteria, PageVo pageVo){
        Pageable pageable = PageRequest.of(pageVo.getPageIndex()-1, pageVo.getPageSize(),
                Sort.Direction.DESC,"id");
        return BaseResult.success(eduLessonService.getList(queryCriteria,pageable));
    }

//    @EnableSysLog("【前台】会员注册")
    @PostMapping("register")
    public BaseResult register(@RequestBody EduMember eduMember){
        eduMemberService.register(eduMember);
        return BaseResult.success("注册成功");
    }

//    @EnableSysLog("【前台】会员登录")
    @PostMapping("login")
    public BaseResult login(@RequestBody EduMember eduMember){
        EduMember dbEduMember = eduMemberService.login(eduMember);
        // 生成token
        String token = HutoolJWTUtil.createToken(dbEduMember);
        dbEduMember.setMemberToken(token);
        return BaseResult.success("登录成功",dbEduMember);
    }
}
